<?php
namespace App\Models\Trade\Search;

use App\Models\Model;

class SearchRecordModel extends Model
{

    const TABLENAME = 'tw_trade_search_record';

    /**
     * 查询一条数据（今天）
     *
     * @param string $name
     * @param array $fields
     * @return \Illuminate\Support\Collection
     */
    public static function findTodayByName($name, array $fields = [])
    {
        $query = self::dbTable();
        if (count($fields)) {
            $query->select($fields);
        }
        $where[] = ['name', $name];
        $where[] = ['timed', strtotime('today -1 days')];
        
        return $query->where($where)->get();
    }

    /**
     * 添加一条数据
     *
     * @param array $data            
     * @return id
     */
    public static function addItem(array $data = [])
    {
        $data['timed'] = strtotime('today -1 days');
        $data['created'] = $data['updated'] = time();
        
        return self::dbTable()->insertGetId($data);
    }

    /**
     * 编辑一条数据
     *
     * @param int $id            
     * @param array $data            
     * @return false | 1 | 0
     */
    public static function editItem($id, array $data = [])
    {
        if (! $id) {
            return false;
        }
        $data['updated'] = time();
        
        return self::dbTable()->where('id', $id)->update($data);
    }
}
